<?php
/********************************************************************
Product		: Flexicontact
Date		: 5 January 2014
Copyright	: Les Arbres Design 2009-2014
Contact		: http://www.lesarbresdesign.info
Licence		: GNU General Public License
*********************************************************************/
defined('_JEXEC') or die('Restricted Access');

class com_flexicontactInstallerScript
{
public function __constructor(JAdapterInstance $adapter) { }
public function preflight($route, JAdapterInstance $adapter) { }
public function install(JAdapterInstance $adapter) { }
public function update(JAdapterInstance $adapter) { }
public function uninstall(JAdapterInstance $adapter)
{ 
	echo "<h2>Flexicontact has been uninstalled</h2>";
}
//-------------------------------------------------------------------------------
// The main installation function
//
public function postflight($route, JAdapterInstance $adapter)
{
// check the Joomla version (JVERSION did not exist before Joomla 2.5)

	$version = new JVersion();
	$joomla_version = $version->RELEASE;

	if ($joomla_version < '2.5')
		{
		echo '<h3>'."Flexicontact cannot run on this old version of Joomla ($joomla_version)".'</h3>';
		return false;
		}
		
	if (substr(JVERSION,0,1) > "3")				// if > 3
		{
		echo "This version of Flexicontact has not been tested on this version of Joomla.";
		return false;
		}
		
// get the component version from the component manifest xml file		

	$component_version = $adapter->get('manifest')->version;

// delete redundant files from older versions

	@unlink(JPATH_SITE.'/administrator/components/com_flexicontact/admin.flexicontact.php');
	@unlink(JPATH_ROOT.'/administrator/components/com_flexicontact/toolbar.flexicontact.html.php'); 
	@unlink(JPATH_ROOT.'/administrator/components/com_flexicontact/toolbar.flexicontact.php'); 
	@unlink(JPATH_ROOT.'/administrator/components/com_flexicontact/admin.flexicontact.html.php');
	@unlink(JPATH_ROOT.'/components/com_flexicontact/flexicontact.html.php');
	@unlink(JPATH_ROOT.'/components/com_flexicontact/RL_flexicontact.html.php');
	@unlink(JPATH_SITE.'/administrator/components/com_flexicontact/joomla15.xml');
	@unlink(JPATH_SITE.'/administrator/components/com_flexicontact/joomla16.xml');

// add new column to the log table, if it exists
// (it doesn't get created unless the user turns logging on)

	$this->_db = JFactory::getDBO();
	if ($this->table_exists('#__flexicontact_log'))
		{
		$this->add_column('#__flexicontact_log', 'admin_email', "VARCHAR(60) NOT NULL DEFAULT '' AFTER `email`");
		$this->add_column('#__flexicontact_log', 'list_choice', "VARCHAR(60) DEFAULT NULL AFTER `browser_string`");
		}

// we are done

	echo "Flexicontact version $component_version installed.";
	return true;
}

//-------------------------------------------------------------------------------
// Check whether a table exists in the database. Returns TRUE if exists, FALSE if it doesn't
//
function table_exists($table)
{
	$tables = $this->_db->getTableList();
	$table = self::replaceDbPrefix($table);
	if (self::in_arrayi($table,$tables))
		return true;
	else
		return false;
}

//-------------------------------------------------------------------------------
// Check whether a column exists in a table. Returns TRUE if exists, FALSE if it doesn't
//
function column_exists($table, $column)
{
	if (version_compare(JVERSION,"3.0.0","<"))	// if < 3.0
		{
		$result = $this->_db->getTableFields($table);
		$fields = &$result[$table];
		}
	else
		{
		$result = $this->_db->getTableColumns($table);
		$fields = &$result;
		}
		
	if ($fields === null)
		return false;
		
	if (array_key_exists($column,$fields))
		return true;
	else
		return false;
}

//-------------------------------------------------------------------------------
// Add a column if it doesn't exist (the table must exist)
//
function add_column($table, $column, $details)
{
	if ($this->column_exists($table, $column))
		return;
	$query = 'ALTER TABLE `'.$table.'` ADD `'.$column.'` '.$details;;
	return $this->ladb_execute($query);
}

//-------------------------------------------------------------------------------
// Replace the generic database prefix #__ with the real one
//
static function replaceDbPrefix($sql)
{
	$app = JFactory::getApplication();
	$dbprefix = $app->getCfg('dbprefix');
	return str_replace('#__',$dbprefix,$sql);
}

//-------------------------------------------------------------------------------
// Case insensitive in_array()
//
static function in_arrayi($needle, $haystack)
{
    return in_array(strtolower($needle), array_map('strtolower', $haystack));
}

//-------------------------------------------------------------------------------
// Execute a SQL query and return true if it worked, false if it failed
//
function ladb_execute($query)
{
	if (version_compare(JVERSION,"3.0.0","<"))	// if < 3.0
		{
		$this->_db->setQuery($query);
		$this->_db->execute();
		if ($this->_db->getErrorNum())
			{
			echo '<div style="color:red">'.$this->_db->stderr().'</div>';
			return false;
			}
		return true;
		}
		
// for Joomla 3.0 use try/catch error handling

	try
		{
		$this->_db->setQuery($query);
		$this->_db->execute();
		}
	catch (RuntimeException $e)
		{
	    echo '<div style="color:red">'.$e->getMessage().'</div>';
		return false;
		}
	return true;
}


} // end of class definition
